package org.jeecg.modules.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.system.entity.SysUserRole;
import org.jeecg.modules.system.service.ISysUserRoleService;
import org.jeecg.modules.system.vo.SysUserRoleVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;

/**
 * <p>
 * 员工角色表 前端控制器
 * </p>
 *
 * @since 2023-07-01
 */
@Slf4j
@RestController
@RequestMapping("/sys/employeeRole")
public class SysEmployeeRoleController {

    @Autowired
    private ISysUserRoleService sysUserRoleService;
    
    /**
     * 给指定角色添加员工（使用员工ID替代用户ID）
     *
     * @param sysUserRoleVO 包含角色ID和员工ID列表
     * @return 处理结果
     */
    @RequestMapping(value = "/addEmployeeRole", method = RequestMethod.POST)
    public Result<String> addEmployeeRole(@RequestBody SysUserRoleVO sysUserRoleVO) {
        Result<String> result = new Result<String>();
        try {
            String sysRoleId = sysUserRoleVO.getRoleId();
            for(String employeeId : sysUserRoleVO.getUserIdList()) {
                SysUserRole sysUserRole = new SysUserRole(employeeId, sysRoleId);
                QueryWrapper<SysUserRole> queryWrapper = new QueryWrapper<SysUserRole>();
                queryWrapper.eq("role_id", sysRoleId).eq("user_id", employeeId);
                SysUserRole one = sysUserRoleService.getOne(queryWrapper);
                if(one == null) {
                    sysUserRoleService.save(sysUserRole);
                }
            }
            result.setMessage("添加成功!");
            result.setSuccess(true);
            return result;
        } catch(Exception e) {
            log.error(e.getMessage(), e);
            result.setSuccess(false);
            result.setMessage("出错了: " + e.getMessage());
            return result;
        }
    }
    
    /**
     * 删除指定角色的员工关系
     * 
     * @param roleId 角色ID
     * @param employeeId 员工ID
     * @return 处理结果
     */
    @RequestMapping(value = "/deleteEmployeeRole", method = RequestMethod.DELETE)
    public Result<SysUserRole> deleteEmployeeRole(@RequestParam(name="roleId") String roleId,
                                                @RequestParam(name="employeeId", required=true) String employeeId) {
        Result<SysUserRole> result = new Result<SysUserRole>();
        try {
            QueryWrapper<SysUserRole> queryWrapper = new QueryWrapper<SysUserRole>();
            queryWrapper.eq("role_id", roleId).eq("user_id", employeeId);
            sysUserRoleService.remove(queryWrapper);
            result.success("删除成功!");
        } catch(Exception e) {
            log.error(e.getMessage(), e);
            result.error500("删除失败！");
        }
        return result;
    }
    
    /**
     * 批量删除指定角色的员工关系
     *
     * @param roleId 角色ID
     * @param employeeIds 员工ID，多个用逗号分隔
     * @return 处理结果
     */
    @RequestMapping(value = "/deleteEmployeeRoleBatch", method = RequestMethod.DELETE)
    public Result<SysUserRole> deleteEmployeeRoleBatch(
            @RequestParam(name="roleId") String roleId,
            @RequestParam(name="employeeIds",required=true) String employeeIds) {
        Result<SysUserRole> result = new Result<SysUserRole>();
        try {
            QueryWrapper<SysUserRole> queryWrapper = new QueryWrapper<SysUserRole>();
            queryWrapper.eq("role_id", roleId).in("user_id", Arrays.asList(employeeIds.split(",")));
            sysUserRoleService.remove(queryWrapper);
            result.success("删除成功!");
        } catch(Exception e) {
            log.error(e.getMessage(), e);
            result.error500("删除失败！");
        }
        return result;
    }
} 